<?php
require_once("../../config/config.inc.php");
//require_once("task_function.php"); 

function findParenttaskNumb($index) {
    global $aTask;
    $ordernumb = "0";

    if ($aTask[$sIndex]["sPID"] == "0") {
        return $ordernumb;
    }
    for ($i=0; $i<count($aTask); $i++) {
        if ($aTask[$i]["sID"] == $aTask[$index]["sPID"]) { 
            $ordernumb = $aTask[$i]["sNumb"];
            break;
        }
    }
    return $ordernumb;
}

//开始定义变量
$sUserID   = $SEC->sUserID;
$sUsername = $SEC->sUserName;
$sRole     = $SEC->sRole;
///$sAction   = fn_getParam("strAction","");
$sProjectID = fn_getParam("strProjectID",""); //$_SESSION["task_project_id"];
$sTaskEditAll = fn_isNull($_SESSION["task_enable_editall"],"none"); //editall

if ($sProjectID == "" && isset($_SESSION["task_project_id"])) {
    $sProjectID = $_SESSION["task_project_id"];
}

////获取项目列表
$sProjectList = "";
$sql = "SELECT ID,PROJECTNAME,USERID,USERNAME,MANAGER ";
$sql.= "FROM $TABLE_PROJECT ";
$sql.= "WHERE (USERNAME='".$SEC->sUserName."' OR MANAGER='".$SEC->sUserName."'";
$sql.= " OR ID IN (SELECT DISTINCT PROJECTID FROM $TABLE_TASK WHERE ID IN (SELECT DISTINCT TASKID FROM $TABLE_TASK_RESOURCE WHERE USERNAME='".$SEC->sUserName."'))";
$sql.= ") ORDER BY PROJECTNAME";
$stmt= $DB->query($sql);
while ($row = $DB->fetchArray($stmt)) {
    $sProjectList .= ",['".$row["ID"]."','".fn_setParam($row["PROJECTNAME"])."']";
    if ($row["ID"] == $sProjectID) {
        $_SESSION["task_project_id"] = $sProjectID;
    }
}
$DB->freeResult($stmt);
if ($sProjectList == "") {
    $sProjectList = "[]";
} else {
    $sProjectList = "[".substr($sProjectList,1)."]";
}
$SM->assign("sProjectList",$sProjectList);
$SM->assign("sProjectID",$sProjectID);

////获取任务列表
//jqtree map规则：顶级任务(没有父任务)的则为0，其他的非顶级任务的则为其父任务的任务号。
$treeMap = "";
if ($sProjectID>0) {
    $aTask = array(array());
    $aPretask = array(array());
    $aResource= array(array());

    $aTaskType = $DICT->getDictArray("BASE_TASK_TYPE",false);
    $aTaskLimitType = $DICT->getDictArray("BASE_TASK_LIMITTYPE",false);

    $sql  = "SELECT A.TASKID, B.ORDERNUMB ";
    $sql .= "FROM $TABLE_PRETASK A,$TABLE_TASK B ";
    $sql .= "WHERE B.PROJECTID='".$sProjectID."' AND A.PRETASKID=B.ID ";
    $sql .= "ORDER BY A.TASKID,B.ORDERNUMB";
    $stmt = $DB->query($sql);
    $sIndex=0;
    while ($row = $DB->fetchArray($stmt)) {
        $aPretask[$sIndex][0] = $row["TASKID"];
        $aPretask[$sIndex][1] = $row["ORDERNUMB"];
        $sIndex++;
    }
    $DB->freeResult($stmt);

    $sql  = "SELECT A.TASKID,A.USERNAME,A.TRUENAME ";
    $sql .= "FROM $TABLE_TASK_RESOURCE A,$TABLE_TASK B ";
    $sql .= "WHERE B.PROJECTID='".$sProjectID."' AND A.TASKID=B.ID ";
    $sql .= "ORDER BY A.TASKID,A.USERNAME";
    $stmt = $DB->query($sql);
    $sIndex=0;
    while ($row = $DB->fetchArray($stmt)) {
        $aResource[$sIndex][0] = $row["TASKID"];
        $aResource[$sIndex][1] = ($row["USERNAME"]=="task_res_other"?$row["TRUENAME"]:$row["USERNAME"]); 
        $sIndex++;
    }
    $DB->freeResult($stmt);

    $sql  = "SELECT ID,USERNAME,ORDERNUMB,PARENTID,WBSCODE,TASKNAME,";
    $sql .= "PRIORITY,DURATION,ISESTIMATE,";
    $sql .= "DATE_FORMAT(PLANSTARTDATE,'%Y-%m-%d %H:%i') AS PLANSTARTDATE,";
    $sql .= "DATE_FORMAT(PLANFINISHDATE,'%Y-%m-%d %H:%i') AS PLANFINISHDATE,";
    $sql .= "DATE_FORMAT(FACTSTARTDATE,'%Y-%m-%d %H:%i') AS FACTSTARTDATE,";
    $sql .= "DATE_FORMAT(FACTFINISHDATE,'%Y-%m-%d %H:%i') AS FACTFINISHDATE,";
    $sql .= "DATE_FORMAT(LIMITDATE,'%Y-%m-%d %H:%i') AS LIMITDATE,";
    $sql .= "DATE_FORMAT(CREATEDATE,'%Y-%m-%d %H:%i:%s') AS CREATEDATE,";
    $sql .= "DATE_FORMAT(UPDATEDATE,'%Y-%m-%d %H:%i:%s') AS UPDATEDATE,";
    $sql .= "FINISHPERCENT,LIMITTYPE,TASKTYPE,ISMILESTONE "; //,TASKINFO,FINISHINFO,REMARK
    $sql .= "FROM $TABLE_TASK ";
    $sql .= "WHERE PROJECTID='".$sProjectID."' ";
    $sql .= "ORDER BY ORDERNUMB";
    $stmt= $DB->query($sql);
    $sIndex =0;
    while ($row = $DB->fetchArray($stmt)) {
        $aTask[$sIndex]["sID"]   = $row["ID"];
        $aTask[$sIndex]["sPID"]  = $row["PARENTID"];
        $aTask[$sIndex]["sNumb"] = $row["ORDERNUMB"];
        $aTask[$sIndex]["sName"] = fn_setParam($row["TASKNAME"]);
        $aTask[$sIndex]["sPri"]  = $row["PRIORITY"];
        $aTask[$sIndex]["sWbs"]  = $row["WBSCODE"];

        $aTask[$sIndex]["sTType"] = $aTaskType[$row["TASKTYPE"]];
        $aTask[$sIndex]["sPSD"]   = fn_fmDatetime($row["PLANSTARTDATE"]);
        $aTask[$sIndex]["sPFD"]   = fn_fmDatetime($row["PLANFINISHDATE"]);
        $aTask[$sIndex]["sLType"] = $aTaskLimitType[$row["LIMITTYPE"]];
        $aTask[$sIndex]["sLDate"] = fn_fmDatetime($row["LIMITDATE"]);
        $aTask[$sIndex]["sFP"]   = $row["FINISHPERCENT"];
        $aTask[$sIndex]["sFSD"]  = fn_fmDatetime($row["FACTSTARTDATE"]);
        $aTask[$sIndex]["sFFD"]  = fn_fmDatetime($row["FACTFINISHDATE"]);
        $aTask[$sIndex]["sDur"]  = $row["DURATION"];
        $aTask[$sIndex]["isEst"] = $row["ISESTIMATE"];
        $aTask[$sIndex]["isMst"] = $row["ISMILESTONE"];

        for ($i=0; $i<count($aPretask)&&count($aPretask[0])>0; $i++) {
            if ($aPretask[$i][0] == $aTask[$sIndex]["sID"]) {
                $aTask[$sIndex]["preTask"] = $aTask[$sIndex]["preTask"].",".$aPretask[$i][1];
            }
        }
        if ($aTask[$sIndex]["preTask"] != "") {
            $aTask[$sIndex]["preTask"] = substr($aTask[$sIndex]["preTask"],1);
        }

        for ($i=0; $i<count($aResource)&&count($aResource[0])>0; $i++) {
            if ($aResource[$i][0] == $aTask[$sIndex]["sID"]) {
                $aTask[$sIndex]["sRes"] = $aTask[$sIndex]["sRes"].",".$aResource[$i][1];
            }
        }
        if ($aTask[$sIndex]["sRes"] != "") {
            $aTask[$sIndex]["sRes"] = substr($aTask[$sIndex]["sRes"],1);
        }
        //find parent task order number
        $treeMap .= ",".findParenttaskNumb($sIndex);

        $sIndex++;
    }
    $DB->freeResult($stmt);
    $DB->close();

    $SM->assign("aTask",$aTask);
    unset($aTask);
    unset($aPretask);
    unset($aResource);
}
if ($treeMap == "") {
    $treeMap = "[0]";
} else {
    $treeMap = "[".substr($treeMap,1)."]";
}
$SM->assign("treeMap",$treeMap);

$SM->display('task/taskList.html');
?>
